<?php

namespace App\Http\Controllers\Admin;

use App\AdminPermission;
use App\AdminRole;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class RoleController extends Controller
{
    #角色列表
    public function index(){
        $roles  = AdminRole::paginate(10);
        return view('admin.role.index', compact('roles'));
    }

    #创建角色页面
    public function create(){
        return view('admin.role.add');
    }

    #创建角色行为
    public function store(){

        $this->validate(request(), [
            'name' => 'required|min:3',
            'description' => 'required',
        ]);

        AdminRole::create(request(['name', 'description']));

        return redirect('/admin/roles');
    }

    #角色和权限关系页面
    public function permission(\App\AdminRole $role){

        $permissions = \App\AdminPermission::all();
        $myPermissions = $role->permissions;
        return view('admin.role.permission' ,compact('permissions', 'myPermissions', 'role'));
    }

    #储存角色行为
    public function storePermission(AdminRole $role){
        $this->validate(request(),[
            'permissions' => 'required|array'
        ]);

        $permissions = \App\AdminPermission::find(request('permissions'));
        $myPermissions = $role->permissions;

        // 对已经有的权限
        $addPermissions = $permissions->diff($myPermissions);
        foreach ($addPermissions as $permission) {
            $role->grantPermission($permission);
        }

        $deletePermissions = $myPermissions->diff($permissions);
        foreach ($deletePermissions as $permission) {
            $role->deletePermission($permission);
        }
        return redirect('/admin/roles');
    }

}
